package com.taotao.order.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.order.pojo.OrderInfo;
import com.taotao.order.service.OrderService;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbUser;
import com.taotao.trolley.service.CartService;

/**
 * 订单页面controller
 * @author Administrator
 *
 */
@Controller
public class OrderController {

	@Value("${TOKEN_KEY}")
	private String TOKEN_KEY;
		
	@Autowired
	private CartService cartService;
	
	@Autowired
	private OrderService orderService;
	
	//href="http://localhost:8092/order/order-cart.html"
	@RequestMapping("/order/order-cart")
	public String showOrder(HttpServletRequest request,HttpServletResponse response,Model model) {
		
		TbUser user = (TbUser) request.getAttribute("USER_INFO");
		List<TbItem> cartList = cartService.getCartList(user.getId());
		model.addAttribute("cartList", cartList);
		return "order-cart";
	}
	
	//order/create.html
	@RequestMapping(value="/order/create",method=RequestMethod.POST)
	public String createOrder(OrderInfo info,HttpServletRequest request) {
		
		TbUser user = (TbUser) request.getAttribute("USER_INFO");
		//设置用户
		info.setBuyerNick(user.getUsername());
		//设置用户Id
		info.setUserId(user.getId());
		TaotaoResult result = orderService.createOrder(info);
		//传递给页面
		request.setAttribute("payment", info.getPayment());
		request.setAttribute("orderId", result.getData());
		DateTime dateTime = new DateTime();
		DateTime plusDays = dateTime.plusDays(3);//加3天
		request.setAttribute("date", plusDays.toString("yyyy-MM-dd"));
		return "success";
	}
	
}
